package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController {
    @RequestMapping("/test1")
    public String fileUpload(User user, MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("=================");

        if (!file.isEmpty()){

            String originalFilename = file.getOriginalFilename();
            String name = file.getName();
            System.out.println("FileUpload running" + file);

            //指定本地上传的服务器
            String realPath = request.getServletContext().getRealPath("/imgs/");
            File file1 = new File(realPath);

            //将文件对象上传到指定到位置的方法
            file.transferTo(new File(realPath, originalFilename));

            //向User对象中存入图片
            user.setPic(originalFilename);

        }else {

            System.out.println("is empty");

        }

        return "fileUpload";
    }

}
